<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>OpenMesh: Using STL algorithms</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="logo_align.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="rwth_vci_rgb.jpg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">OpenMesh
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('a04339.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Using STL algorithms </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>Since the OpenMesh iterators are (almost) conformant to STL iterators, one can apply the STL algorithms on meshes.</p>
<p>The following example shows how to use the STL <code>for_each</code> construct, since it is easier to read and may be more efficient than hand-written loops.</p>
<p>We will define a class which provides the smoothing algorithm, hence define a reusable component. The class must be template class because there is no such thing as a class OpenMesh, but many different types of OpenMesh:</p>
 <div class="fragment"><div class="line"><span class="keyword">template</span> &lt;<span class="keyword">class</span> Mesh&gt; <span class="keyword">class </span>SmootherT</div></div><!-- fragment --></p>
<p>The class SmootherT has two functors, one that computes the barycenter for a given vertex, and a second that sets the vertex position to the corresponding barycenter. A functor is simply a class with a function <code>operator()(...)</code>. The first functor <code>ComputeCOG</code> computes the barycenter and stores it in a custom vertex property <code>cog_:</code> </p>
<p><div class="fragment"><div class="line">    <span class="keywordtype">void</span> operator()(<span class="keyword">const</span> <span class="keyword">typename</span> Mesh::VertexHandle&amp; _vh)</div><div class="line">    {</div><div class="line">      <span class="keyword">typename</span> Mesh::VertexVertexIter  vv_it;</div><div class="line">      <span class="keyword">typename</span> Mesh::Scalar            valence(0.0);</div><div class="line">    </div><div class="line">      mesh_.property(cog_, _vh) = <span class="keyword">typename</span> Mesh::Point(0.0, 0.0, 0.0);</div><div class="line"></div><div class="line">      <span class="keywordflow">for</span> (vv_it=mesh_.vv_iter(_vh); vv_it.is_valid(); ++vv_it)</div><div class="line">      {</div><div class="line">        mesh_.property(cog_, _vh) += mesh_.point( *vv_it );</div><div class="line">        ++valence;</div><div class="line">      }</div><div class="line"></div><div class="line">      mesh_.property(cog_, _vh ) /= valence;</div><div class="line">    }</div></div><!-- fragment --></p>
<p>Note, that <code>ComputeCOG</code> needs to have access to the mesh object and the property handle. Here, both are references to member variables of the smoother object.</p>
<p>The second functor <code>class</code> <code>SetCOG</code>, which sets the vertex position, is constructed analogical.</p>
<p>Using these functors and <code>std::for_each</code> from the STL the smoothing algorithm can be realized in a member function of <code>SmootherT:</code> </p>
 <div class="fragment"><div class="line">  <span class="keywordtype">void</span> smooth(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> _iterations)</div><div class="line">  {</div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i &lt; _iterations; ++i)</div><div class="line">    {</div><div class="line">      std::for_each(mesh_.vertices_begin(), </div><div class="line">                    mesh_.vertices_end(), </div><div class="line">                    ComputeCOG(mesh_, cog_));</div><div class="line"></div><div class="line">      std::for_each(mesh_.vertices_begin(), </div><div class="line">                    mesh_.vertices_end(), </div><div class="line">                    SetCOG(mesh_, cog_));</div><div class="line">    }</div><div class="line">  }</div></div><!-- fragment --></p>
<p><br />
The complete example looks like this:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;algorithm&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/Utils/Property.hh&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#ifndef DOXY_IGNORE_THIS</span></div><div class="line"></div><div class="line"><span class="keyword">template</span> &lt;<span class="keyword">class</span> Mesh&gt; <span class="keyword">class </span>SmootherT</div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line"></div><div class="line">  <span class="keyword">typedef</span> <span class="keyword">typename</span> Mesh::Point            cog_t;</div><div class="line">  <span class="keyword">typedef</span> <a class="code" href="a02553.html">OpenMesh::VPropHandleT&lt; cog_t &gt;</a> Property_cog;</div><div class="line"></div><div class="line"><span class="keyword">public</span>:</div><div class="line"></div><div class="line">  <span class="comment">// construct with a given mesh</span></div><div class="line">  <span class="keyword">explicit</span> SmootherT(Mesh&amp; _mesh) </div><div class="line">    : mesh_(_mesh)</div><div class="line">  { </div><div class="line">    mesh_.add_property( cog_ );</div><div class="line">  }</div><div class="line"></div><div class="line">  ~SmootherT()</div><div class="line">  {</div><div class="line">    mesh_.remove_property( cog_ );</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// smooth mesh _iterations times</span></div><div class="line">  <span class="keywordtype">void</span> smooth(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> _iterations)</div><div class="line">  {</div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i &lt; _iterations; ++i)</div><div class="line">    {</div><div class="line">      std::for_each(mesh_.vertices_begin(), </div><div class="line">                    mesh_.vertices_end(), </div><div class="line">                    ComputeCOG(mesh_, cog_));</div><div class="line"></div><div class="line">      std::for_each(mesh_.vertices_begin(), </div><div class="line">                    mesh_.vertices_end(), </div><div class="line">                    SetCOG(mesh_, cog_));</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">private</span>:</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">//--- private classes ---</span></div><div class="line"></div><div class="line">  <span class="keyword">class </span>ComputeCOG</div><div class="line">  {</div><div class="line">  <span class="keyword">public</span>:</div><div class="line">    ComputeCOG(Mesh&amp; _mesh, Property_cog&amp; _cog) </div><div class="line">      : mesh_(_mesh), cog_(_cog)</div><div class="line">    {}</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> operator()(<span class="keyword">const</span> <span class="keyword">typename</span> Mesh::VertexHandle&amp; _vh)</div><div class="line">    {</div><div class="line">      <span class="keyword">typename</span> Mesh::VertexVertexIter  vv_it;</div><div class="line">      <span class="keyword">typename</span> Mesh::Scalar            valence(0.0);</div><div class="line">    </div><div class="line">      mesh_.property(cog_, _vh) = <span class="keyword">typename</span> Mesh::Point(0.0, 0.0, 0.0);</div><div class="line"></div><div class="line">      <span class="keywordflow">for</span> (vv_it=mesh_.vv_iter(_vh); vv_it.is_valid(); ++vv_it)</div><div class="line">      {</div><div class="line">        mesh_.property(cog_, _vh) += mesh_.point( *vv_it );</div><div class="line">        ++valence;</div><div class="line">      }</div><div class="line"></div><div class="line">      mesh_.property(cog_, _vh ) /= valence;</div><div class="line">    }</div><div class="line"></div><div class="line">  <span class="keyword">private</span>:</div><div class="line">    Mesh&amp;         mesh_;</div><div class="line">    Property_cog&amp; cog_;</div><div class="line">  };</div><div class="line"></div><div class="line"></div><div class="line">  <span class="keyword">class </span>SetCOG</div><div class="line">  {</div><div class="line">  <span class="keyword">public</span>:</div><div class="line">    SetCOG(Mesh&amp; _mesh, Property_cog&amp; _cog) </div><div class="line">      : mesh_(_mesh), cog_(_cog)</div><div class="line">    {}</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> operator()(<span class="keyword">const</span> <span class="keyword">typename</span> Mesh::VertexHandle&amp; _vh)</div><div class="line">    {</div><div class="line"></div><div class="line">      <span class="keywordflow">if</span> (!mesh_.is_boundary(_vh))</div><div class="line">        mesh_.set_point( _vh, mesh_.property(cog_, _vh) );</div><div class="line">    }</div><div class="line"></div><div class="line">  <span class="keyword">private</span>:</div><div class="line"></div><div class="line">    Mesh&amp;         mesh_;</div><div class="line">    Property_cog&amp; cog_;</div><div class="line">  };</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">//--- private elements ---</span></div><div class="line"></div><div class="line">  Mesh&amp;        mesh_;</div><div class="line">  Property_cog cog_;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="preprocessor">#endif</span></div></div><!-- fragment --><p><br />
and</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;vector&gt;</span></div><div class="line"><span class="comment">// -------------------- OpenMesh</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/IO/MeshIO.hh&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh&gt;</span></div><div class="line"><span class="comment">// -------------------- </span></div><div class="line"><span class="preprocessor">#include &quot;smooth_algo.hh&quot;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// ----------------------------------------------------------------------------</span></div><div class="line"></div><div class="line"><span class="preprocessor">#ifndef DOXY_IGNORE_THIS</span></div><div class="line"></div><div class="line"><span class="keyword">struct </span><a class="code" href="a01881.html">MyTraits</a> : <span class="keyword">public</span> <a class="code" href="a02469.html">OpenMesh::DefaultTraits</a></div><div class="line">{</div><div class="line">  HalfedgeAttributes(<a class="code" href="a01238.html#ab78a93560926cd2f9958cb028f7ea96dafaaebe1808b5cce96ad4e19df471d58a">OpenMesh::Attributes::PrevHalfedge</a>);</div><div class="line">};</div><div class="line"></div><div class="line"><span class="preprocessor">#endif</span></div><div class="line"></div><div class="line"><span class="keyword">typedef</span> <a class="code" href="a02377.html">OpenMesh::TriMesh_ArrayKernelT&lt;MyTraits&gt;</a>  MyMesh;</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// ----------------------------------------------------------------------------</span></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)</div><div class="line">{</div><div class="line">  MyMesh  mesh;</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// check command line options</span></div><div class="line">  <span class="keywordflow">if</span> (argc != 4) </div><div class="line">  {</div><div class="line">    std::cerr &lt;&lt; <span class="stringliteral">&quot;Usage:  &quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; #iterations  infile  outfile\n&quot;</span>;</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">  }</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// read mesh from stdin</span></div><div class="line">  <span class="keywordflow">if</span> ( ! <a class="code" href="a01236.html#ac417960ec28ffdd7e7003a2b1ebb2d1c">OpenMesh::IO::read_mesh</a>(mesh, argv[2]) )</div><div class="line">  {</div><div class="line">     std::cerr &lt;&lt; <span class="stringliteral">&quot;Error: Cannot read mesh from &quot;</span> &lt;&lt; argv[2] &lt;&lt; std::endl;</div><div class="line">     <span class="keywordflow">return</span> 1;</div><div class="line">  }</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// smoothing mesh argv[1] times</span></div><div class="line">  SmootherT&lt;MyMesh&gt; smoother(mesh);</div><div class="line">  smoother.smooth(atoi(argv[1]));</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// write mesh to stdout</span></div><div class="line">  <span class="keywordflow">if</span> ( ! <a class="code" href="a01236.html#a18286a792ebf896872f5e26182fc5c7b">OpenMesh::IO::write_mesh</a>(mesh, argv[3]) )</div><div class="line">  {</div><div class="line">    std::cerr &lt;&lt; <span class="stringliteral">&quot;Error: cannot write mesh to &quot;</span> &lt;&lt; argv[3] &lt;&lt; std::endl;</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">  }</div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<hr>
<address>
<small>
<a href="http://www.rwth-graphics.de" style="text-decoration:none;">
</a>
Project <b>OpenMesh</b>, 
&copy;&nbsp; Computer Graphics Group, RWTH Aachen.
Documentation generated using 
<a class="el" href="http://www.doxygen.org/index.html">
<b>doxygen</b>
</a>.
</small>
</address>
</body>
</html>
